package com.jy.modules.cims.interact;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.data.common.dao.TCMmDealInterfaceDao;
import com.jy.modules.cims.data.common.dto.TCMmDealInterfaceDTO;
import com.jy.modules.cims.data.dao.SysAssistDao;
import com.jy.modules.cims.data.dto.DealInterfaceDto;

/**
 * <b>类名称：</b>CInteractAssist <br/>
 * <b>类描述：接口辅助类</b><br/>
 * <b>创建人：</b>chenguixue<br/>
 * <b>修改人：</b><br/>
 * <b>修改时间：</b>2015-2-1 下午2:06:18<br/>
 * <b>修改备注：</b><br/>
 * <b>版本信息：</b>v1.0.0<br/>
 */
@Service("com.jy.modules.cims.interact.CInteractAssist")
public class CInteractAssist implements IInteractAssist {

    //private static final Logger logger = LoggerFactory.getLogger(CInteractAssist.class);

    @Autowired
    public SysAssistDao sysAssistDao;

    @Autowired
    public TCMmDealInterfaceDao tcmmDealInterfaceDao;

    @Autowired
    @Qualifier("com.jy.modules.cims.interact.CIRLoanIFImpl")
    CIRLoanIFImpl cIRLoanIFImpl;

    /**
     * 生成接口调用的唯一流水号
     */
    public String getInteractNo() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * @author chenguixue
     * @date 2015-2-2 上午9:55:35
     * @see com.jy.modules.cims.interact.IInteractAssist#getBusiNoForInteract()
     */
    public String getBusiNoForInteract() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * @author chenguixue
     * @throws ParseException
     * @date 2015-2-1 上午10:44:19
     * @see com.jy.modules.cims.core.service.sysAssist.ICSysAssist#registerDealIrtService(java.lang.String)
     */
    @Override
    public void registerDealIrtService(String bizNo) {
        // 增加校验核心日终状态
       /* CoreStatusResponse csr;
        csr = cIRLoanIFImpl.queryCoreStatus(DateUtil.getFormatDate(new java.util.Date()));
        if (null == csr) {
            logger.error(CimsExceptionConstant.CORE_EXCEPTION);
            throw new CoreEndException(CimsExceptionConstant.CORE_EXCEPTION);
        }
        if ("0".equals(csr.getCoreEndStatus())) {
            logger.error(CimsExceptionConstant.CORE_END);
            throw new CoreEndException(CimsExceptionConstant.CORE_END);
        }*/
        // 记录接口流水信息频繁，所有查询都记录接口不妥，2-3个月数据几千万，记录信息不全，没有发送和接收信息，查询该表信息作用不大，可以暂时屏蔽该代码。
        // registerDealIrtService(bizNo, CimsConstant.INTERACT_NUM_ONE);
    }

    /**
     * @author chenguixue
     * @date 2015-2-1 上午11:00:52
     * @see com.jy.modules.cims.core.service.sysAssist.ICSysAssist#registerDealIrtService(java.lang.String,
     *      int)
     */
    @Override
    public void registerDealIrtService(String bizNo, int num) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        DealInterfaceDto dto = new DealInterfaceDto();
        dto.setDealCode(bizNo);
        paramMap.put("dto", dto);
        // List<DealInterfaceDto> list =
        // sysAssistDao.searchDealInterfaceForUpdate(paramMap);

        TCMmDealInterfaceDTO newDto = null;
        for (int i = 1; i <= num; i++) {
            newDto = new TCMmDealInterfaceDTO();
            paramMap.put("dto", newDto);
            newDto.setDealCode(bizNo);
            newDto.setFronttransno(getInteractNo());
            newDto.setIrttime(new Timestamp(new Date().getTime()));
            newDto.setSerialno(Long.valueOf(i));
            newDto.setStatus(CimsConstant.INTERACT_IMPL_FALSE);
            tcmmDealInterfaceDao.insertTCMmDealInterface(paramMap);
        }
    }

    @Override
    public String registerDealIrtService() {
        String bizNo = getInteractNo();
        registerDealIrtService(bizNo, CimsConstant.INTERACT_NUM_ONE);
        return bizNo;
    }

}
